package middleware

import (
	"github.com/gin-gonic/gin"
	"openapi.itspire.cn/common/dict"
	"openapi.itspire.cn/config"
	"openapi.itspire.cn/database"
	"openapi.itspire.cn/model"
	"openapi.itspire.cn/utils"
)

func Token() gin.HandlerFunc {
	return func(c *gin.Context) {
		var user model.User
		token := c.Request.Header.Get("itspire-token")
		if token != "" {
			userId := utils.AesDecrypt(token, config.Conf.App.AESKey, "CBC")
			database.Ins.Where("id = ? AND delete_time = ?", userId, dict.DeleteNot).First(&user)
		}
		utils.SetUserInfo(c, user)

		c.Next()
	}
}
